using System;
using System.Data;
using System.Collections.Generic;
using System.Text;

namespace AIFSDataAccessLayer.Utilities
{
    public static class DataTableUtil
    {
        /// <summary>
        /// Helper method to add a column to the table if it does not exist already. If it does returns the datacolumn.
        /// </summary>
        public static DataColumn AddNewColumn(DataTable table, string columnName, Type dataType)
        {
            DataColumn dc = null;

            if (table != null)
            {
                if (table.Columns.Contains(columnName))
                    dc = table.Columns[columnName];
                else
                {
                    dc = new DataColumn(columnName, dataType);
                    dc.DefaultValue = System.DBNull.Value;
                    table.Columns.Add(dc);
                }
            }

            return dc;
        }

        public static bool? ToBoolType(DataRow aRow, string columnName)
        {
            object objVal = aRow[columnName];
            if (objVal == System.DBNull.Value)
                return null;
            return (bool?)objVal;
        }

        public static bool? ToBoolType(DataRow aRow, string columnName, bool considerNullAsFalse)
        {
            bool? val = ToBoolType(aRow, columnName);
            if (!val.HasValue && considerNullAsFalse)
                return false;
            else
                return val;
        }

        public static int? ToIntType(DataRow aRow, string columnName)
        {
            object objVal = aRow[columnName];
            if (objVal == System.DBNull.Value)
                return null;
            return (int?)objVal;
        }

        public static System.Int16? ToInt16Type(DataRow aRow, string columnName)
        {
            object objVal = aRow[columnName];
            if (objVal == System.DBNull.Value)
                return null;
            return System.Int16.Parse(objVal.ToString());
        }

        public static double? ToDoubleType(DataRow aRow, string columnName)
        {
            object objVal = aRow[columnName];
            if (objVal == System.DBNull.Value)
                return null;
            return double.Parse(objVal.ToString());
        }

        public static decimal? ToDecimalType(DataRow aRow, string columnName)
        {
            object objVal = aRow[columnName];
            if (objVal == System.DBNull.Value)
                return null;
            return decimal.Parse(objVal.ToString());
        }


        public static object ToObjectType(DataRow aRow, string columnName)
        {
            object objVal = aRow[columnName];
            if (objVal == System.DBNull.Value)
                return null;

            return objVal;
        }

        public static string ToStringType(DataRow aRow, string columnName)
        {
            object objVal = aRow[columnName];
            if (objVal == System.DBNull.Value)
                return null;

            return (string)objVal;
        }

        public static DateTime? ToDateTimeType(DataRow aRow, string columnName)
        {
            object objVal = aRow[columnName];
            if (objVal == System.DBNull.Value)
                return null;

            return (DateTime)objVal;
        }        
    }
}
